﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _00_窗体应用程序
{
    public partial class Form1 : Form
    {
        //储存窗体的初始大小
        private float formOriginalWidth;
        private float formOriginalHeight;
        //存储所有控件的初始位置和大小
        private Dictionary<Control, Rectangle> controlsInitialInfo= new Dictionary<Control, Rectangle>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 fr2 = new Form2();
            fr2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 fr2 = new Form2();
            fr2.ShowDialog();
        }
        /// <summary>
        /// 当窗体大小改变的的时候触发的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Resize(object sender, EventArgs e)
        {
            this.Text = "我的第一个窗体=> Width:" +this.Width + "Height:"+ this.Height ;

            //计算缩放比例
            float scaleWidth = (float)this.Width / formOriginalWidth;
            float scaleHeight = (float)this.Height / formOriginalHeight;

            //调整所有控件的大小和位置
            foreach (Control control in this.Controls)
            {
                Rectangle originalRect = controlsInitialInfo[control];
                //调整参数
                control.Left = (int)(originalRect.Left * scaleWidth);
                control.Top = (int)(originalRect.Top * scaleHeight);
                control.Width = (int)(originalRect.Width * scaleWidth);
                control.Height = (int)(originalRect.Height * scaleHeight);
            }

        }
        /// <summary>
        /// 窗体还没有显示之前加载的方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            formOriginalWidth = this.Width;
            formOriginalHeight = this.Height;

            //保存所有控件的初始位置和大小
            foreach (Control control in this.Controls)
            {
                Rectangle rectangle = new Rectangle(control.Location.X, control.Location.Y, control.Width, control.Height);
                controlsInitialInfo.Add(control, rectangle);
            }
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Resize += new System.EventHandler(this.Form1_Resize);
            this.WindowState = FormWindowState.Maximized;

        }
    }
}
